import aiohttp
import asyncio
from utils.HBaseConnect import HBaseConnect
import uuid
import time
import schedule
import re

from utils.MysqlConnect import MysqlConnect

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 "
                  "Safari/537.36 Edg/125.0.0.0",
    "Cookie": "SCF=Ag_vn0DNsrgB8JeKaZgA-cuTOPa3xzPWTAsmdzKkPXAKVrZrzUCxEYNhtwaXFWSUBm_WMKyL9atOg7nChNgoimU.; "
              "ALF=1718270163; _T_WM=56789772923; WEIBOCN_FROM=1110006030; MLOGIN=0; "
              "BAIDU_SSP_lcr=https://login.sina.com.cn/; XSRF-TOKEN=9bbc20; "
              "M_WEIBOCN_PARAMS=oid%3D5040453242063228%26luicode%3D10000011%26lfid%3D106003type%253D25%2526t%253D3"
              "%2526disable_hot%253D1%2526filter_type%253Drealtimehot%26fid%3D106003type%253D25%2526t%253D3"
              "%2526disable_hot%253D1%2526filter_type%253Drealtimehot%26uicode%3D10000011",
}
tableName = "hot_search_keywords"


def save_hot_search_keywords(hbase, mysql, dic):
    print("排名" + dic['rank'], dic)
    hbase.putTable(tableName, rowKey=str(uuid.uuid1().hex), data={
        "info:keyword": dic['keyword'],
        "info:num": dic['searchNum'],
        "info:rank": dic['rank'],
        "info:url": dic['url'],
        "info:platform": "微博",
        "info:time": dic['time'],
    })
    insert_sql = (
        "INSERT INTO information(hot_keyword, search_num, ranking, url, platform,time) "
        "VALUES(%s, %s, %s, %s, %s, %s)")
    insert_args = (dic['keyword'], dic['searchNum'], dic['rank'], dic['url'],"微博",dic['time'])
    mysql.insert(insert_sql, insert_args)

# 获取热搜关键词
async def get_hot_search_keywords():
    try:
        # 初始化hbase的参数
        hbase = HBaseConnect()
        hbase.start()
        mysql = MysqlConnect()
        current_time = time.strftime('%Y-%m-%d %H:%M:%S')
        url = ("https://m.weibo.cn/api/container/getIndex?sudaref=cn.bing.com&display=0&retcode=6102&containerid"
               "=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&page_type=08")
        async with aiohttp.ClientSession(headers=headers) as session:
            async with session.get(url) as res:
                resp = await res.json()
                dic = {"time": current_time}
                rank = 1
                items = resp['data']['cards'][0]['card_group']
                for item in items:
                    if item.get('desc_extr') and item['desc_extr'] != '':
                        dic['keyword'] = item["desc"]
                        # 使用正则表达式提取数字部分
                        number = re.search(r'\d+', str(item["desc_extr"])).group()
                        dic['searchNum'] = number
                        dic['url'] = item["scheme"]
                        dic['rank'] = str(rank)
                        save_hot_search_keywords(hbase, mysql, dic)
                        rank += 1
                hbase.stop()
                # 关闭mysql
                mysql.cur.close()
                mysql.connection.close()
    except Exception as e:
        print("Exception", e)


def run_asyncio_task():
    print("当前时间", time.strftime('%Y-%m-%d %H:%M:%S'))
    asyncio.run(get_hot_search_keywords())


if __name__ == '__main__':
    # 在每小时的00, 10, 20, 30, 40, 50分钟运行任务
    schedule.every().hour.at(":00").do(run_asyncio_task)
    schedule.every().hour.at(":10").do(run_asyncio_task)
    schedule.every().hour.at(":20").do(run_asyncio_task)
    schedule.every().hour.at(":30").do(run_asyncio_task)
    schedule.every().hour.at(":40").do(run_asyncio_task)
    schedule.every().hour.at(":50").do(run_asyncio_task)
    while True:
        schedule.run_pending()
        time.sleep(1)
